package com.dj.common.web.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.dj.common.web.rest.ResponsePack;
import com.dj.common.web.rest.RestApiError;

public class CoreCrudRestController
		extends CoreBaseRestController {

	public String defultError = "error";
	protected ModelAndView getModelAndView() {
		return new ModelAndView();
	}
	/**
     * 
     * 方法描述 : 使用@InitBinder 标签对表单数据绑定
     * @param binder
     */
    @InitBinder//spring mvc使用@InitBinder 标签对表单数据绑定
    public void initBinder(WebDataBinder binder) {//WebDataBinder是用来绑定请求参数到指定的属性编辑器
    	 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
         dateFormat.setLenient(false);    
         binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
	protected HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		return request;
	}
	protected HttpServletResponse getResponse() {
		HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getResponse();
		return response;
	}
	/**
	 * 得到PageData
	 */
	public Map<String,Object> getParams() {
		Map<String, String[]> properties = this.getRequest().getParameterMap();
		Map<String,Object> map = new HashMap<>();
		Iterator<String> itr = properties.keySet().iterator();
		String name = "";
		String value = "";
		while (itr.hasNext()) {
			name = itr.next();
			value = "";
			Object valueObj = properties.get(name);
			if (null == valueObj) {
				value = "";
			} else if (valueObj instanceof String[]) {
				String[] values = (String[]) valueObj;
				for (int i = 0; i < values.length; i++) {
					value += values[i] + ",";
				}
				value = value.substring(0, value.length() - 1);
			} else {
				try {
					value = URLDecoder.decode(valueObj.toString(), "UTF-8");
				} catch (UnsupportedEncodingException e) {
				};
			}
			map.put(name, value);
		}
		return map;
	}
	public void returnError(int status,String message){
		try{
			HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
					.getResponse();
			RestApiError rae = new RestApiError();
			rae.setCode(status+"");
			rae.setMessage(message);
			ResponsePack<JSONObject> pp = new ResponsePack<>(rae);
			ServletOutputStream out = response.getOutputStream();
			out.write(JSONObject.toJSONString(pp).getBytes("UTF-8"));
			out.flush();
			out.close();
		}catch (Exception ex) {}
	}
}
